<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商" prop="orderSupId">
        <el-select v-model="queryParams.orderSupId" placeholder="请输入供应商" clearable>
          <el-option
            v-for="dict in dict.type.durg_support_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="申请人" prop="orderMan">
        <el-input
          v-model="queryParams.orderMan"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="单据状态" prop="detailStatus">
        <el-select v-model="queryParams.detailStatus" placeholder="单据状态" clearable>
          <el-option
            v-for="dict in dict.type.durg_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['system:order:add']">
          <router-link :to="{name:'order'}" class="link-type">
            新增采购
          </router-link>
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >提交审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >作废
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="putStorage"
          v-hasPermi="['system:order:export']"
        >提交入库
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="单据ID" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/drug/order-data/index/' + scope.row.orderId" class="link-type">
            <span>{{ scope.row.orderDocumentId }}</span>
          </router-link>
        </template>
      </el-table-column>
      <!--      <el-table-column label="单据ID" align="center" prop="orderDocumentId" />-->
      <el-table-column label="供应商" align="center" prop="orderSupId" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.durg_support_type" :value="scope.row.orderSupId"/>
        </template>
      </el-table-column>
      <el-table-column label="采购批发总额" align="center" prop="orderTotal"/>
      <el-table-column label="状态" align="center" prop="detailStatus" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.durg_order_status" :value="scope.row.detailStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="orderMan"/>
      <el-table-column label="入库操作人" align="center" prop="detailOperator"/>
      <el-table-column label="入库时间" align="center" prop="detailDate"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单
对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据ID" prop="orderDocumentId">
          <el-input v-model="form.orderDocumentId" placeholder="请输入单据ID"/>
        </el-form-item>
        <el-form-item label="供应商" prop="orderSupId">
          <el-input v-model="form.orderSupId" placeholder="请输入供应商"/>
        </el-form-item>
        <el-form-item label="采购批发总额" prop="orderTotal">
          <el-input v-model="form.orderTotal" placeholder="请输入采购批发总额"/>
        </el-form-item>
        <el-form-item label="申请人" prop="orderMan">
          <el-input v-model="form.orderMan" placeholder="请输入申请人"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/drug/order";

export default {
  name: "Order",
  dicts: ['durg_support_type', 'durg_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //被选中的状态信息
      detailStatuses: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderDocumentId: null,
        orderSupId: null,
        orderTotal: null,
        orderMan: null,
        detailStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderDocumentId: [
          {required: true, message: "单据ID不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderDocumentId: null,
        orderSupId: null,
        orderTotal: null,
        orderMan: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.detailStatuses = selection.map(item => item.detailStatus)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 提交审核按钮操作 */
    handleUpdate() {
      const orderId = this.ids[0];
      const detailStatus = this.detailStatuses[0];
      if (detailStatus !== "0") {
        this.$modal.msgError("只允许选择申请状态进行审核操作!");
        return
      }
      updateOrder({orderId: orderId, detailStatus: 1}).then(response => {
        this.$modal.msgSuccess("申请审核成功!");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      const detailStatus = this.detailStatuses;
      this.$confirm('是否确认作废订单编号为"' + orderIds + '"的数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let flag = false;
        detailStatus.forEach(status => {
          if (status === "3") {
            this.$modal.msgError("审核成功的订单不允许作废!");
            flag = true;
          } else if (status === "4") {
            this.$modal.msgError("入库成功的订单不允许作废!");
            flag = true;
          }
        })
        //证明不包含入库成功和已入库的订单信息
        if(!flag){
          orderIds.forEach(id => {
            updateOrder({orderId: id, detailStatus: 5})
          })
          this.$modal.msgSuccess("作废订单成功");
          this.getList();
        }
      }).catch(() => {
    });
  },
    /*提交入库功能*/
    putStorage(row){
      const orderIds = row.orderId || this.ids;
      const detailStatus = this.detailStatuses;
      this.$confirm('确认提交订单编号为"' + orderIds + '"的数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let flag = false;
        detailStatus.forEach(status => {
          if (status !== "3") {
            this.$modal.msgError("只允许审核成功的订单入库!");
            flag = true;
          }
        })
        //证明不包含入库成功和已入库的订单信息
        if(!flag){
          orderIds.forEach(id => {
            updateOrder({orderId: id, detailStatus: 4})
          })
          this.$modal.msgSuccess("入库订单成功");
          this.getList();
        }
      }).catch(() => {
      });

    }
  },
  //监听其他页面跳转行为 如果是订单表 则重新查询列表
  watch: {
    $route() {
      console.log(this.$router)
      if (this.$router.history.current.name === "Order") {
        this.getList();
      }
    },
  },
};
</script>
